<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>教师管理系统</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: "Segoe UI", sans-serif;
            background-color: #f4f6f9;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 40px;
        }

        .container {
            width: 100%;
            max-width: 900px;
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            display: flex;
            flex-direction: column;
            gap: 30px;
        }

        h2, h3 {
            color: #333;
            text-align: center;
        }

        .form-container {
            background-color: #f9f9f9;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }

        .form-container input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .form-container label {
            font-weight: bold;
            display: block;
            margin-top: 10px;
        }

        .form-container button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
        }

        .form-container button:hover {
            background-color: #0056b3;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        th, td {
            text-align: left;
            padding: 12px;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #007bff;
            color: white;
        }

        tr:hover {
            background-color: #f1f1f1;
        }

        .action-buttons button {
            margin-right: 5px;
            padding: 6px 10px;
            font-size: 14px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
        }

        .edit-btn {
            background-color: #ffc107;
            color: black;
        }

        .delete-btn {
            background-color: #dc3545;
            color: white;
        }
    </style>
</head>
<body>

<div class="container">

    <!-- 教师列表 -->
    <div>
        <h3>📚 教师列表</h3>
        <table id="teacherTable">
            <thead>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>所属系别</th>
                <th>邮箱</th>
                <th>联系电话</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>

    <!-- 表单区域 -->
    <div class="form-container">
        <input type="hidden" id="teacherNo">
        <label for="name">姓名：</label>
        <input type="text" id="name">

        <label for="department">所属系别：</label>
        <input type="text" id="department">

        <label for="email">邮箱：</label>
        <input type="email" id="email">

        <label for="phone">联系电话：</label>
        <input type="text" id="phone">

        <button onclick="saveTeacher()">保存教师</button>
        <button onclick="clearForm()">清空表单</button>
    </div>

</div>

<script>
    $(document).ready(function () {
        loadTeachers();
    });

    function loadTeachers() {
        $.ajax({
            url: "/api/teachers",
            method: "GET",
            success: function (data) {
                var tbody = $("#teacherTable tbody");
                tbody.empty();
                data.forEach(function (t) {
                    tbody.append(`
                    <tr>
                        <td>${t.teacherNo}</td>
                        <td>${t.name}</td>
                        <td>${t.department}</td>
                        <td>${t.email}</td>
                        <td>${t.phone}</td>
                        <td class="action-buttons">
                            <button class="edit-btn" onclick='editTeacher(${JSON.stringify(t)})'>编辑</button>
                            <button class="delete-btn" onclick='deleteTeacher(${t.teacherNo})'>删除</button>
                        </td>
                    </tr>
                `);
                });
            }
        });
    }

    function saveTeacher() {
        var teacher = {
            teacherNo: $("#teacherNo").val(),
            name: $("#name").val(),
            department: $("#department").val(),
            email: $("#email").val(),
            phone: $("#phone").val()
        };

        var url = "/api/teachers";
        var method = teacher.teacherNo ? "PUT" : "POST";

        if (teacher.teacherNo) {
            url += "/" + teacher.teacherNo;
        }

        $.ajax({
            url: url,
            method: method,
            contentType: "application/json",
            data: JSON.stringify(teacher),
            success: function () {
                loadTeachers();
                clearForm();
            }
        });
    }

    function editTeacher(teacher) {
        $("#teacherNo").val(teacher.teacherNo);
        $("#name").val(teacher.name);
        $("#department").val(teacher.department);
        $("#email").val(teacher.email);
        $("#phone").val(teacher.phone);
    }

    function deleteTeacher(id) {
        if (confirm("确定要删除吗？")) {
            $.ajax({
                url: "/api/teachers/" + id,
                method: "DELETE",
                success: function () {
                    loadTeachers();
                }
            });
        }
    }

    function clearForm() {
        $("#teacherNo").val("");
        $("#name").val("");
        $("#department").val("");
        $("#email").val("");
        $("#phone").val("");
    }
</script>

</body>
</html>